package repository

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
	"lib/structs/lms"
	"lib/structs/tables"
	"lib/utils"
	"lms/global"
)

var CoursewareDb = new(coursewareDb)

type coursewareDb struct {
}

// DeleteFolder 删除目录
func (r coursewareDb) DeleteFolder(folderId int64) (err error) {
	err = global.DB.Delete(&tables.CoursewareFolder{FolderId: folderId}).Error
	return
}

// FindLastCode 查询今天生成的最后一个code。
func (r coursewareDb) FindLastCode() (code string, err error) {
	err = global.DB.Model(&tables.Courseware{}).Select("code").Where("created_at > ?", utils.GetToday0().Unix()).
		Order("created_at desc").Limit(1).Find(&code).Error
	return
}

// CheckCode 检查课件code是否已经被使用。b-true 没有被使用；
func (r coursewareDb) CheckCode(code string) (b bool, err error) {
	var num int64
	err = global.DB.Model(&tables.Courseware{}).Where(tables.Courseware{Code: code}).Count(&num).Error
	if err != nil {
		return
	}
	b = num < 1
	return
}

// FindCoursewareListByCourseId 获取课程关联的课件列表
func (r coursewareDb) FindCoursewareListByCourseId(courseId int64) (li []tables.Courseware, err error) {
	err = global.DB.Model(&tables.Courseware{}).Where("id in "+
		" (select lms_map_courseware.courseware_id from lms_map_courseware where course_id = ? ) ", courseId).Find(&li).Error
	return
}

// UpdateCoursewareVectorStatus 批量更新课件向量化状态
func (r coursewareDb) UpdateCoursewareVectorStatus(ids []int64, status int) (err error) {
	err = global.DB.Exec("update lms_courseware set vector_status = ? where id in ? ",
		status, ids).Error
	return err
}

func (r coursewareDb) FindById(id int64) (m tables.Courseware, err error) {
	err = global.DB.Model(&tables.Courseware{}).Where(&tables.Courseware{Id: id}).First(&m).Error
	return
}

func (r coursewareDb) DeleteById(id int64) (err error) {
	err = global.DB.Delete(&tables.Courseware{Id: id}).Error
	return
}

func (r coursewareDb) FindByIds(ids []int64) (li []tables.Courseware, err error) {
	err = global.DB.Model(&tables.Courseware{}).Where("id in ?", ids).Find(&li).Error
	return
}

func (r coursewareDb) FindSelfByIds(ids []int64, uid int64) (li []tables.Courseware, err error) {
	err = global.DB.Model(&tables.Courseware{}).Where("id in ? AND creator_id = ?", ids, uid).Find(&li).Error
	return
}

// FindCoursewareByName 检查同目录下名称是否已经被使用，false 未被使用
func (r coursewareDb) FindCoursewareByName(name string, parentId int64) (error, bool) {
	m := new(tables.CoursewareFolder)
	err := global.DB.Where(&tables.CourseFolder{Name: name, ParentId: parentId}).First(&m).Error
	if errors.Is(gorm.ErrRecordNotFound, err) {
		return nil, false
	}
	return err, true
}

func (r coursewareDb) CreateCoursewareFolder(m *tables.CoursewareFolder) (err error) {
	err = global.DB.Save(&m).Error
	return
}

func (r coursewareDb) Save(m *tables.Courseware) (err error) {
	err = global.DB.Save(m).Error
	return
}

func (r coursewareDb) FindAll() (li []tables.Courseware, err error) {
	err = global.DB.Find(&li).Error
	return
}

// FindAllCoursewareFolder 获取所有课件目录
func (r coursewareDb) FindAllCoursewareFolder() (li []tables.CoursewareFolder, err error) {
	err = global.DB.Find(&li).Error
	return
}

// FindPage 获取课件分页信息
func (r coursewareDb) FindPage(in lms.SearchCourseware, folderIds []int64, uid int64) (li []tables.Courseware, total int64, err error) {
	tx := global.DB.Model(&tables.Courseware{}).Where("courseware_folder_id in ?", folderIds)
	if in.Name != "" {
		tx.Where("name like ?", fmt.Sprintf("%%%s%%", in.Name))
	}
	if in.Code != "" {
		tx.Where("code like ?", fmt.Sprintf("%%%s%%", in.Code))
	}
	if in.IsMe {
		tx.Where("creator_id = ?", uid)
	} else {
		tx.Where("is_public = 1 or creator_id = ?", uid)
	}
	err = tx.Count(&total).Offset((in.PageNum - 1) * in.PageSize).Limit(in.PageSize).Find(&li).Error
	return
}
